package com.jt.serviceImpl;

import com.jt.mapper.SearchMapper;
import com.jt.pojo.SearchResult;
import com.jt.service.SearchService;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author dc
 * @Classname SearchServiceImpl
 * @Date 2020/3/27 10:55
 */
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchMapper searchMapper;

    @Override
    public SearchResult findItemByKey(String key,int page,int rows) {
        //创建solr查询对象
        SolrQuery query=new SolrQuery(key);
        //设置起始位置以及查询条数,并根据更新时间降序
        int startIndex=(page-1)*rows;
        query.setStart(startIndex);
        query.setRows(rows);
        query.setSort("updated", SolrQuery.ORDER.desc);
        //设置默认搜索域
        query.set("df", "title");
        //设置高亮
        query.setHighlight(true);
        query.addHighlightField("title");
        query.setHighlightSimplePre("<b><em style=\"color:red\">");
        query.setHighlightSimplePost("</em></b>");
        //设置查询最长的字符串
        query.setHighlightFragsize(50);
        SearchResult result =searchMapper.search(query);
        //计算查询结果总页数
        long recordCount=result.getRecordCount();
        long pageCount=recordCount/rows;
        if(recordCount%rows>0){
            pageCount++;
        }
        result.setPageCount(pageCount);
        result.setCurrentPage(page);
        return result;
    }
}
